import cgi
import os
import logging
import base64
import authorization

from collections import deque
from xml.etree import ElementTree as ET
from google.appengine.ext.webapp import template
from models import model
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db

class Moves(webapp.RequestHandler):
    def post(self, board_name):
        
        headers = self.request.headers
        user = authorization.authorization(headers)
        logging.error(user)
        if(user == ""):
          self.error(403)
        else:
          root = ET.XML(self.request.body)
          create_move = root[0]
          find_x_coordinate = create_move.find('row')
          find_y_coordinate = create_move.find('column')
          if(find_x_coordinate != None and find_y_coordinate != None):
            x_coordinate = find_x_coordinate.attrib.get("value")
            y_coordinate = find_y_coordinate.attrib.get("value")
            logging.error(x_coordinate)
            logging.error(y_coordinate)
          else:
            x_coordinate = ""
            y_coordinate = ""

          board = model.Boards.all().filter('Name = ', board_name).fetch(limit=1)
          if(board == []):
            self.error(404)
          else:
            if(board[0].Owner != user and board[0].Invitee != user):
              self.error(401)
            else:
              moves = model.Board_moves.all().filter('Board = ', board_name)
              count = moves.count()

              move = model.Board_moves()
              move.Board = board_name
              move.Sequence_number = count + 1
              move.X_coordinate = x_coordinate
              move.Y_coordinate = y_coordinate
              move.put()

    def get(self, board_name):

        start = self.request.get("start")
        end = self.request.get("end")
        last = self.request.get("last")
        
        moves = model.Board_moves.all().filter('Board = ', board_name).order("Created_at")
        count = moves.count()

        to = count
        start_from = 1

        if(last != "" and (start != "" or end != "")):
          self.error(406) #not acceptable
        else:
          if(end != "" and start == ""):
            moves = moves.fetch(limit=int(end))
            start_from=1
            to=end
          if(start != "" and end == ""):
            moves = moves.fetch(limit=count,offset=int(start)-1)
            start_from=start
            to=count
          if(last != ""):
            if(count == 0):
              moves = moves.fetch(limit=1)
            else:
              moves = moves.fetch(limit=count, offset=count - int(last))
            start_from = count - int(last) + 1
            to = count
          if(start != "" and end != ""):
            moves = moves.fetch(limit=int(end)-int(start)+1,offset=int(start)-1)
            start_from=start
            to=end

          template_values = {
            'count': count,
            'board_name': board_name,
            'end': to,
            'start': start_from,
            'moves': moves,
          }

          path = os.path.join(os.path.dirname(__file__), 'templates/moves.html')

          self.response.headers['Content-Type'] = 'text/xml'
          self.response.out.write(template.render(path, template_values))


